package com.dsit.sq;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dsit_dev04 on 2016/10/12.
 */
public class ChangeWifiActivity extends BaseActivity implements OnClickListener {
    private ImageView returnImageView;
    private ImageView hideImageView;
    private Button modifyButton;
    private boolean isHide = false;
    private EditText passwordEditText, usernameEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarBackChange.setStatusBarBack(this);
        setContentView(R.layout.activity_changewifi);
        returnImageView = (ImageView) findViewById(R.id.wifi_setting_return_button);
        returnImageView.setOnClickListener(this);
        hideImageView = ((ImageView) findViewById(R.id.hide_password));
        hideImageView.setOnClickListener(this);
        hideImageView.setBackgroundResource(R.mipmap.hide_password_normal);

        passwordEditText = (EditText) findViewById(R.id.password);
        usernameEditText = (EditText) findViewById(R.id.username);
        modifyButton = (Button) findViewById(R.id.modifly_btn);
        modifyButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wifi_setting_return_button:
                finish();
                break;
            case R.id.hide_password:
                if (isHide) {
                    passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    hideImageView.setBackgroundResource(R.mipmap.hide_password_presssd);
                } else {
                    passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    hideImageView.setBackgroundResource(R.mipmap.hide_password_normal);
                }
                isHide = !isHide;
            case R.id.modifly_btn:
                if (App.instance.deviceConnected) {
                    //热点已连接
                    Boolean nameCondition =  usernameEditText.getText().toString().length() < 4;
                    Boolean passwordContion = passwordEditText.getText().toString().length() < 6;
                    if (nameCondition) {
                        Toast.makeText(ChangeWifiActivity.this, "请输入最少4位的WiFi名称！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (passwordContion){
                        Toast.makeText(ChangeWifiActivity.this, "请输入最少6位的密码！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //发送要修改的WiFi名字和密码到机器

                } else {
                    //热点断开
                    Toast.makeText(ChangeWifiActivity.this, "请先连上机器热点！", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    public boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
